#include "engine.h"


inline void Util::set_syspath(char* rootpath) {
	generic_object tmp;
	generic_data   tdata;
	tdata.isNumber = 0; tdata.store.sData = string(rootpath);
	tmp.name = "SYS";
	tmp.stringStore["PATH"] = tdata;
	Util::config.repository["SYS"] = tmp;
}


inline int Util::LUA_get_mouse_loc(lua_State *L) {
	int x,y;
	SDL_GetMouseState(&x,&y);
	lua_pushnumber(L,y); lua_pushnumber(L,x);
	return 2;
}

inline int Util::LUA_ismouse_pressed(lua_State *L) {
	int state,btnum;
	if(!lua_isnumber(L,ARG1_1)) { lua_pushnumber(L,-1); return 1; }
	btnum = (int)lua_tonumber(L,ARG1_1);
	state = SDL_GetMouseState(NULL,NULL);
	if(btnum < 1 || btnum > 5) { lua_pushnumber(L,-2); return 1; }
	lua_pushnumber(L,SDL_BUTTON(btnum) & state);
	return 1;
}

inline int Util::LUA_iskey_pressed(lua_State *L) {
	
	Uint8 *keystate; int keynum;

	if(!lua_isnumber(L,ARG1_1)) { lua_pushnumber(L,-1); return 1; }
	keystate = SDL_GetKeyState(NULL);
	if( keynum <= SDLK_FIRST || keynum >= SDLK_LAST) { lua_pushnumber(L,-2); return 1; }
	lua_pushnumber(L,keystate[ keynum ]);
	return 1;
}

/*
static void Util::create_binding(char *filepath,BaseModule &mapStore) {
	TiXmlDocument doc(filepath);
	TiXmlNode *node;
	TiXmlElement *todoElement,*element; TiXmlElement *xmlMapname,*xmlMapfn; string map_1,map_2;
	node = doc.RootElement();
	if(!node) return;
	if(strcmp(node->Value(),"mapping") != 0) return;
	todoElement = node->ToElement();
	for(element = todoElement->FirstChildElement(); element; element = element->NextSiblingElement() )  {
		if(strcmp(element->Value() , "map") != 0) continue;
		xmlMapName = get_element(element,"tag");
		xmlMapfn   = get_element(element,"fn");
		if(!(xmlMapName && xmlMapfn)) continue;
		map_1 = xmlMapName->GetText(); map_2 = xmlMapfn->GetText();
		mapStore.mapping_tag_fn[ map_1 ]  = map_2;

	}



}


inline static TiXmlElement* Util::get_element(TiXmlElement* xmlElem,char *sText) {
		TiXmlElement *element;

	for( element = root->FirstChildElement(); element; element = element->NextSiblingElement() ) {

		if(strcmp(sText,element->Value()) == 0) return element;
	}
	return NULL;

}
*/
